package com.edu.exam.entity.grading;

import com.edu.exam.enums.common.WorkflowStatus;
import com.edu.exam.enums.common.QualityLevel;
import com.edu.exam.enums.quality.monitoring.PriorityLevel;
import com.edu.exam.enums.common.SeverityLevel;
import com.edu.exam.enums.grading.QualityCheckType;
import com.edu.exam.enums.grading.QualityCheckStatus;
import com.edu.exam.enums.grading.QualityGrade;
import com.edu.exam.enums.grading.QualityCheckMethod;
import com.edu.exam.enums.grading.QualityCheckScope;
import com.edu.exam.enums.grading.QualityReviewStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

/**
 * 质量检查实体类
 * [所属模块] E-04 扫描阅卷处理模块
 *
 * 根据E-04-05 网上批阅子模块设计实现
 * 对应quality_check数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class QualityCheck {

    @Id
    @Column(name = "check_id", length = 64)
    private String checkId;

    /**
     * 任务ID
     */
    @Column(name = "task_id", nullable = false, length = 64)
    private String taskId;

    /**
     * 教师ID
     */
    @Column(name = "teacher_id", nullable = false, length = 64)
    private String teacherId;

    /**
     * 检查类型：AUTOMATIC（自动检查）、MANUAL（人工检查）、PEER_REVIEW（同行评审）、SUPERVISOR_REVIEW（主管评审）、SAMPLE_CHECK（抽样检查）
     * TODO: 需要创建专用的CheckType枚举
     */
    @Column(name = "check_type", nullable = false, length = 32)
    private String checkType;

    /**
     * 检查状态：PENDING（待检查）、IN_PROGRESS（检查中）、COMPLETED（已完成）、FAILED（失败）、REVIEWING（审核中）、APPROVED（已批准）、REJECTED（已拒绝）
     * 映射到WorkflowStatus枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "check_status", nullable = false, length = 32)
    private WorkflowStatus checkStatus;

    /**
     * 总分（0-100）
     */
    @Column(name = "total_score", nullable = false, precision = 5, scale = 2)
    private Double totalScore;

    /**
     * 质量等级：EXCELLENT（优秀）、GOOD（良好）、FAIR（中等）、POOR（差）、UNACCEPTABLE（不可接受）
     * 映射到QualityLevel枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "quality_grade", length = 16)
    private QualityLevel qualityGrade;

    /**
     * 检查指标（JSON格式）
     */
    @Column(name = "check_metrics", nullable = false, length = 2000, columnDefinition = "TEXT")
    private String checkMetrics;

    /**
     * 发现问题（JSON格式）
     */
    @Column(name = "issues_found", length = 3000, columnDefinition = "TEXT")
    private String issuesFound;

    /**
     * 改进建议
     */
    @Column(name = "improvement_suggestions", length = 2000, columnDefinition = "TEXT")
    private String improvementSuggestions;

    /**
     * 检查人ID
     */
    @Column(name = "checker_id", length = 64)
    private String checkerId;

    /**
     * 检查时间
     */
    @Column(name = "check_time")
    private LocalDateTime checkTime;

    /**
     * 检查耗时（分钟）
     */
    @Column(name = "check_duration_minutes")
    private Integer checkDurationMinutes;

    /**
     * 检查标准版本
     */
    @Column(name = "check_standard_version", length = 32)
    private String checkStandardVersion;

    /**
     * 检查方法：RULE_BASED（基于规则）、ML_BASED（基于机器学习）、HYBRID（混合）、EXPERT_JUDGMENT（专家判断）
     */
    @Column(name = "check_method", length = 32)
    private String checkMethod;

    /**
     * 检查算法
     */
    @Column(name = "check_algorithm", length = 100)
    private String checkAlgorithm;

    /**
     * 置信度（0-1）
     */
    @Column(name = "confidence_level", precision = 3, scale = 2)
    private Double confidenceLevel;

    /**
     * 检查范围：FULL（全面）、PARTIAL（部分）、SAMPLING（抽样）、TARGETED（定向）
     */
    @Column(name = "check_scope", length = 16)
    private String checkScope;

    /**
     * 采样率（0-1）
     */
    @Column(name = "sampling_rate", precision = 3, scale = 2)
    private Double samplingRate;

    /**
     * 检查样本数量
     */
    @Column(name = "sample_count")
    private Integer sampleCount;

    /**
     * 总检查数量
     */
    @Column(name = "total_check_count")
    private Integer totalCheckCount;

    /**
     * 严重问题数量
     */
    @Column(name = "critical_issues_count")
    private Integer criticalIssuesCount;

    /**
     * 主要问题数量
     */
    @Column(name = "major_issues_count")
    private Integer majorIssuesCount;

    /**
     * 次要问题数量
     */
    @Column(name = "minor_issues_count")
    private Integer minorIssuesCount;

    /**
     * 建议改进数量
     */
    @Column(name = "improvement_suggestions_count")
    private Integer improvementSuggestionsCount;

    /**
     * 优先级：LOW（低）、MEDIUM（中）、HIGH（高）、CRITICAL（严重）
     * 映射到PriorityLevel枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "priority", length = 16)
    private PriorityLevel priority;

    /**
     * 风险等级：LOW（低）、MEDIUM（中）、HIGH（高）、CRITICAL（严重）
     * 映射到SeverityLevel枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "risk_level", length = 16)
    private SeverityLevel riskLevel;

    /**
     * 影响范围：INDIVIDUAL（个人）、TEAM（团队）、DEPARTMENT（部门）、ORGANIZATION（组织）
     */
    @Column(name = "impact_scope", length = 16)
    private String impactScope;

    /**
     * 预期改进效果
     */
    @Column(name = "expected_improvement", length = 1000, columnDefinition = "TEXT")
    private String expectedImprovement;

    /**
     * 实际改进效果
     */
    @Column(name = "actual_improvement", length = 1000, columnDefinition = "TEXT")
    private String actualImprovement;

    /**
     * 后续行动计划
     */
    @Column(name = "follow_up_action_plan", length = 2000, columnDefinition = "TEXT")
    private String followUpActionPlan;

    /**
     * 后续检查时间
     */
    @Column(name = "follow_up_check_time")
    private LocalDateTime followUpCheckTime;

    /**
     * 负责人ID
     */
    @Column(name = "responsible_person_id", length = 64)
    private String responsiblePersonId;

    /**
     * 截止时间
     */
    @Column(name = "deadline")
    private LocalDateTime deadline;

    /**
     * 是否需要培训
     */
    @Column(name = "requires_training")
    private Boolean requiresTraining;

    /**
     * 培训建议
     */
    @Column(name = "training_recommendations", length = 1000, columnDefinition = "TEXT")
    private String trainingRecommendations;

    /**
     * 是否需要工具支持
     */
    @Column(name = "requires_tool_support")
    private Boolean requiresToolSupport;

    /**
     * 工具建议
     */
    @Column(name = "tool_recommendations", length = 1000, columnDefinition = "TEXT")
    private String toolRecommendations;

    /**
     * 历史对比数据（JSON格式）
     */
    @Column(name = "historical_comparison", length = 2000, columnDefinition = "TEXT")
    private String historicalComparison;

    /**
     * 基准对比数据（JSON格式）
     */
    @Column(name = "benchmark_comparison", length = 2000, columnDefinition = "TEXT")
    private String benchmarkComparison;

    /**
     * 同行对比数据（JSON格式）
     */
    @Column(name = "peer_comparison", length = 2000, columnDefinition = "TEXT")
    private String peerComparison;

    /**
     * 趋势分析数据（JSON格式）
     */
    @Column(name = "trend_analysis", length = 2000, columnDefinition = "TEXT")
    private String trendAnalysis;

    /**
     * 检查报告URL
     */
    @Column(name = "report_url", length = 500)
    private String reportUrl;

    /**
     * 附件URL列表（JSON格式）
     */
    @Column(name = "attachment_urls", length = 1000, columnDefinition = "TEXT")
    private String attachmentUrls;

    /**
     * 审核状态：PENDING（待审核）、APPROVED（已批准）、REJECTED（已拒绝）、REVISED（已修订）
     */
    @Column(name = "review_status", length = 16)
    private String reviewStatus;

    /**
     * 审核人ID
     */
    @Column(name = "reviewer_id", length = 64)
    private String reviewerId;

    /**
     * 审核时间
     */
    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    /**
     * 审核意见
     */
    @Column(name = "review_comments", length = 1000, columnDefinition = "TEXT")
    private String reviewComments;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version", nullable = false)
    private Integer version;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的批阅任务
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private OnlineGradingTask gradingTask;

    }
